package com.yuyuan.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.yuyuan.domain.Category;
import com.yuyuan.domain.Plant;
import com.yuyuan.service.PlantService;

@Controller
public class PlantController {
	@Autowired
	private PlantService plantService;
	
	@RequestMapping("list")
	public String list(Plant plant,@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="3")Integer pageSize,Model model){
		PageInfo<Plant> info=plantService.list(plant, pageNum, pageSize);
		model.addAttribute("info", info);
		model.addAttribute("plant", plant);
		return "list";
	}
	
	//批量删除
	@ResponseBody
	@PostMapping("deleteBatch")
	public boolean deleteBatch(@RequestParam("ids[]")Integer[] ids){
		return plantService.deleteBatch(ids)>0;
	}
	//去修改页面
	@GetMapping("update")
	public String update(Integer id,Model model){
		Plant plant=plantService.get(id);
		model.addAttribute("plant", plant);
		return "update";
	}
	//执行修改
	@ResponseBody
	@PostMapping("update")
	public boolean update(Plant plant){
		return plantService.update(plant)>0;
	}
	
	@GetMapping("get")
	public String get(Integer id,Model model){
		Plant plant=plantService.get(id);
		model.addAttribute("plant", plant);
		return "plant";
	}
	@ResponseBody
	@GetMapping("getCategorys")
	public List<Category> getCategorys(){
		return plantService.getCategorys();
	}
	
	@GetMapping("add")
	public String add(){
		return "add";
	}
	@ResponseBody
	@PostMapping("add")
	public boolean add(Plant plant){
		return plantService.add(plant)>0;
	}
	
	
}
